<include file="Common/common" />
<style type="text/css">
	.color{
		color: rgb(240,222,112);
	}
	.button{
		color: rgb(240,222,112);background: rgb(57,61,73);font-size: 16px
	}
</style>
<body style="background: url(../../../../Public/img/otherBackground.png) no-repeat;background-size: cover">
	<div class="layui-tab-item layui-show">
		<div class="layui-main">
			<br>
			<form class="layui-form" id="upload-form" action="">
				<div class="layui-form-item color">
					<label class="layui-form-label">凭证</label>
					<input type="hidden" id="pic_url" name="pic_url" value="{$orderkeyInfo['pic_url']}">
					<div class="layui-upload">
						<button type="button" class="layui-btn button" id="upload-one-pic">添加图片</button>
						
					</div>
					<div class="layui-upload-list" style="margin-left: 110px;">
						<img class="layui-upload-img" id="one-pic" <if condition="$orderkeyInfo['pic_url'] neq ''"> src="{$orderkeyInfo['pic_url']}"</if> >
						<p id="demoText"></p>
					</div>
				</div>

				<input type="hidden" name="orderid" value="{$orderkeyInfo['id']}">

				<div class="layui-form-item">
					<div class="layui-input-block" style="margin-left: 105px;">
						<button  class="layui-btn button" lay-submit="" lay-filter="btn-picurl">上传</button>
					</div>
				</div>
			</form>
		</div>
	</div>
</body>
<script type="text/javascript">
	layui.use(['upload','layedit'], function(){
		var form = layui.form,
		layer = layui.layer,
		layedit = layui.layedit;

		var $ = layui.jquery
		,upload = layui.upload;

		form.render();

		//监听提交表单
		form.on('submit(btn-picurl)', function(data){
			if (data.field.pic_url == '') {
				layer.msg('<span style="color: rgb(240,222,112);">未上传凭证</span>');
				return false;
			}

			layer.confirm('确定要提交？',{offset:'none'},function(index){
				$.ajax({
		            type: "post",
		            url: "{:U('Order/ctrl_submitKeyPic')}",
		            data: data.field,
		            dataType: "json",
		            success: function(rtJson){
                        layer.msg('<span style="color: rgb(240,222,112);">'+rtJson.msg+'</span>');
		               	if (rtJson.code == 1) {
		               		location.href = "{:U('Order/buykey')}";
		               	}
		            }
		        });
			});
			
	        return false;
		});

		//上传凭证
		var uploadInst = upload.render({
			elem: '#upload-one-pic',
			url: "{:U('Common/UploadPic')}",
			before: function(obj){
				//预读本地文件示例，不支持ie8
				obj.preview(function(index, file, result){
					$('#one-pic').attr('src', result); //图片链接（base64）
				});
			},
			done: function(res){
				
				if(res.code == 1){
					//上传成功
					document.getElementById("pic_url").value = res.url;
                    return layer.msg('<span style="color: rgb(240,222,112);">上传成功</span>');
				}else{
					//如果上传失败
                    return layer.msg('<span style="color: rgb(240,222,112);">上传失败</span>');
				}
				
			},
			error: function(){
				//演示失败状态，并实现重传
				var demoText = $('#demoText');
				demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
				demoText.find('.demo-reload').on('click', function(){
					uploadInst.upload();
				});
			}
		});
	});
</script>